package controller;

/**
 * Controller class is used to out questions, recomedations and for transfer entered
 * information
 *
 * @version 1 10 jun 2012
 * @author Lyuda
 */
import accumulator.Accumulator;
import advisor.*;
import analyzer.Consultation;
import java.util.List;
import java.util.Scanner;
import pokemon.Human;
import questionLibrary.*;

public class Controller {

    private Human _pokemon;
    private AdvancedAdvisor _consultant;
    private QuestionLibrary _questionLibrary;
    private QuestionInfo _question;
    private Accumulator _accumulator;
    private Scanner _scanner;
    private OrganInfo _organEffect;

    public Controller(Human pokemon) {
        this._pokemon = pokemon;
        this._questionLibrary = new QuestionLibrary();
        this._accumulator = new Accumulator();
        this._consultant = new AdvancedAdvisor(this._pokemon);
        this._consultant.addAdvisors(new BrainAdvisor(this._pokemon));
        this._consultant.addAdvisors(new LiverAdvisor(this._pokemon));
        this._scanner = new Scanner(System.in);
    }

    public void outputQuestions() {
        String[] answers;
        for (int numberOfQuestion = 0; numberOfQuestion < this._questionLibrary.getCountOfQuestions(); numberOfQuestion++) {
            this._question = this._questionLibrary.getQuestion(numberOfQuestion);
            System.out.println(this._question.getQuestion());
            answers = this._question.getAnswers();
            for (int numberOfAnswer = 0; numberOfAnswer < answers.length; numberOfAnswer++) {
                System.out.println(" " + numberOfAnswer + " " + answers[numberOfAnswer]);
            }
            handlAnswer();
        }
        this._accumulator.get_sum_arr();
    }

    private void handlAnswer() {
        int[] answers;
        try {
            answers = convertInputDataIntoDigit(_scanner.next());
            _organEffect = countEffect(answers);
            _accumulator.add_arr(_organEffect);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    private int[] convertInputDataIntoDigit(String input) {
        String[] answers = input.split(",");
        int[] result = new int[answers.length];
        for (int numberOfAnswer = 0; numberOfAnswer < answers.length; numberOfAnswer++) {
            result[numberOfAnswer] = Integer.parseInt(answers[numberOfAnswer]);
        }
        return result;
    }

    private OrganInfo countEffect(int[] answers) {
        OrganInfo result = new OrganInfo();
        for (int numberOfOrgan = 0; numberOfOrgan < SomeConstants.COUNT_OF_ORGANS; numberOfOrgan++) {
            for (int numberOfParam = 0; numberOfParam < SomeConstants.COUNT_OF_PARAMS; numberOfParam++) {
                result.setParameter(numberOfOrgan, numberOfParam, _question.getEffect(numberOfOrgan, numberOfParam, answers));
            }
        }
        return result;
    }

    public void getRecomedation() {
        try {
            List<Consultation> consultations = this._consultant.getConsultations();
            for (Consultation consultation : consultations) {
                System.out.println(consultation.toString());
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}